// Created by liusc on 2020/8/21-14:24.

import { getObj, getItem, getItem2 } from "@/views/wy-budget/api/budgettemplate.js";
import { getItemObj } from "@/views/wy-budget/api/budgettemplateitem.js";

export default {
    name: "template_look",

    data () {
        return {
            itemValueList: [],
            form: {
                budgetTemplate: {
                    templateId: '',
                    templateName: '',
                    remarks: '',

                },
                budgetTemplateItem: {
                    itemId: '',
                    parentId: '',
                    templateId: '',
                    templateName: '',
                    itemName: '',
                    itemWarn: '',
                    isNead: '0',
                    isLeaf: '',
                    Warning: '',
                },
            },

            rules: {
            },
            currentNodekey: ""
        }
    },

    computed: {
    },

    mounted () {
    },

    created () {
        const loading = this.$loading({
            lock: true,
            text: '数据加载中'
        });

        setTimeout(() => {
            getObj(this.$route.params.templateId).then(respData => {
                this.$refs['add-form'].resetFields()
                this.form.budgetTemplate = respData.data.data
                this.initItem(this.form.budgetTemplate.templateId);

                loading.close()
            }).catch(e => {
                loading.close()
                this.$message({
                    type: 'error',
                    message: '数据获取失败!'
                })
            })
        }, 200)

    },

    methods: {

        handleCheckChange(data, checked) {
            getItemObj(data.itemId).then(res => {
                this.form.budgetTemplateItem = res.data.data
                this.currentNodekey = data.itemId
                this.$refs.tree.setCurrentKey(data.itemId);
            }).catch((err) => {
            });
        },
        // 获取科目
        initItem () {
            getItem2(this.form.budgetTemplate.templateId).then(rep => {
                let rspD = rep.data.data
                if (rspD.length) { 
                    this.handleCheckChange(rspD[0])
                }
                this.itemValueList = rspD
            })
        },

        // 取消
        cancel () {
            this.closeNowRouter()
        },

        closeNowRouter () {
            //关闭当前新增页，暂时没找到好办法
            this.$store.commit("delHistoryRoute", this.$route.fullPath);
            //跳转
            this.$router.push({ name: 'template_list' })
        },

    }
}
